package usermanager

class ExtJsService {

    static transactional = true

    def serviceMethod() {
		return 1;
    }
	def updateData(def params) {
		if(params.nodeType!=null)
		{
			switch (params.nodeType) {
				case "company":
					def foundCompany = Company.get(params.nodeId)
					foundCompany.companyName = params.name
					foundCompany.save()
					return [success: true]
					break
				case "unit":
					def foundUnit = Unit.get(params.nodeId)
					foundUnit.unitName = params.name
					if(foundUnit.save()){
						return [success: true]
					} else {
						[success:false,error : foundUnit.errors]
					}
					break
				case "project":
					def foundProject = Project.get(params.nodeId)
					foundProject.projectName = params.name
					if(foundProject.save()) {
						return [success: true]
					} else {
						[success:false,error : foundProject.errors]
					}
					break
				case "user":
					def foundPerson = Person.get(params.nodeId)
					foundPerson.properties = params
					if(foundPerson.save()) {
						return [success: true]
					} else {
						[success:false,error: foundPerson.errors]
					}
					break
				default:
					break
			}
		}
	}
	def createData(def params) {
		if (params.parentid!=null) {
			switch (params.parentid.split(" ")[0]) {
				case "company":
					def unit = new Unit(unitName:params.name, companies: Company.get(params.parentid.split(" ")[1]))
					if (unit.save()) {
						return [success: true, returnedId: "unit " + unit.id]
					} else {
						return [success: false, error: unit.errors]
					}
					break;
				case "unit":
					def proj = new Project(projectName:params.name, units: Unit.get(params.parentid.split(" ")[1]))
					if(proj.save()) {
						return [success: true, returnedId: "project " + proj.id]
					} else {
						return [success: false, error:proj.errors]
					}
					break;
				case "project":
					def proj = Project.get(params.parentid.split(" ")[1])
					def pers = new Person()
					pers.properties = params
					pers.projects = proj
					pers.projectName = proj.projectName
					if(pers.save()) {
						return [success: true, returnedId: "user " + pers.id]
					 } else {
					 	return [success: false, error: pers.errors]
					 }
					break;
				default:
					break;
					return [success: true]
			}
		}
	}
	def getTreeData(def node) {
		switch (node[0]) {
			case "root":
				def companies = Company.list().collect {comp ->
					return [id: "company "+comp.id, text : comp.companyName,expanded : true]
				}
				return companies
				break
			case "company" :
				def units = Unit.findAllByCompanies(Company.get(node[1].toInteger())).collect {uni ->
					return [id: "unit "+uni.id, text : uni.unitName, parentid : "company " + uni.companies.id]
				}
				return units
				break
			case "unit":
				def projects = Project.findAllByUnits(Unit.get(node[1].toInteger())).collect {pro ->
					return [id: "project " + pro.id, text : pro.projectName, parentid : "unit " + pro.units.id]
				}
				return projects
				break
			case "project":
				def users = Person.findAllByProjects(Project.get(node[1].toInteger())).collect {use ->
					return [id : "user " + use.id, text : use.firstName + " " + use.lastName, parentid: "project " + use.projects.id, leaf:true]
				}
				return users
				break
			default:
				return [:]
				break
		}
	}
	def getInfoPanelData(def node) {
		switch (node[0]) {
			case "user":
				def user =  Person.get(node[1].toInteger())
				def sendUser = [id : user.id, firstName : user.firstName, lastName : user.lastName, email: user.email, home : user.home,
					mobile : user.mobile, bio2: user.bio2, projectName : user.projects.projectName]
				return sendUser 
				break
			case "project":
				def proj =  Project.get(node[1].toInteger())
				def sendProject = [id : proj.id, name : proj.projectName, parentName: proj.units.unitName ]
				return sendProject 
				break
			case "unit":
				def unit =  Unit.get(node[1].toInteger())
				def sendUnit = [id : unit.id, name : unit.unitName, parentName: unit.companies.companyName ]
				return sendUnit 
				break
			case "company":
				def company =  Company.get(node[1].toInteger())
				def sendCompany = [id : company.id, name : company.companyName]
				return sendCompany 
				break
			default:
				return [:] 
				break
		}
	}
	def deleteData(def node) {
		switch (node[0]) {
			case "unit":
				def unitId = node[1]
				Unit.get(unitId).delete()
				return "Unit was deleted"
				break
			case "project":
				def projectId = node[1]
				Project.get(projectId).delete()
				return "Project was deleted"
				break
			case "user":
				def userId = node[1]
				def foundUser = Person.get(userId)
				foundUser.delete();
				return "User was deleted"
				break
			default:
				return ""
		}
	}
}
